package com.graduation.rescuematerial.controller;


import com.graduation.rescuematerial.mapper.UserMapper;
import com.graduation.rescuematerial.service.UserService;
import com.graduation.rescuematerial.vo.RespBean;
import com.graduation.rescuematerial.vo.RespBeanEnum;
import com.graduation.rescuematerial.vo.UpdatePersonalInfo;
import com.graduation.rescuematerial.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/12 21:54
 * @Version:
 * @Description:用户模块
 */
@Slf4j
@RestController
@RequestMapping("user")
@Api(tags = "用户模块")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    //有条件查询（数据库可能会增加工号字段）：
        //1、条件包含姓名，手机号，邮箱，工号（id作为工号）
        //2、注意在部门下的话，条件强制增加了该部门下的查询

    //更改信息（默认用户手机号不可更改，部门名称不可更改）
        //1、更改用户基本信息（用户可自己更改自己的基本信息）
        //2、更改用户的权限
        //3、更改部门基本信息（放在部门模块）

    //核心模块（物资模块）
    @ApiOperation("获取已登录用户的基本信息")
    @PostMapping("/personal")
    @ResponseBody
    public RespBean getPersonalInfo(@RequestBody String ticket, HttpServletRequest request, HttpServletResponse response){
        if(ticket !=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            //这里可能会为空，所以需要判断
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(null!=userVo){
                UserVo user=userMapper.getUserAllInfoByPhone(String.valueOf(userVo.getUserPhone()));
                if(null!=user){
                    return RespBean.success(user);
                }
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("修改个人信息")
    @PostMapping("/update")
    @ResponseBody
    public RespBean updateInfo(@RequestBody UpdatePersonalInfo updatePersonalInfo, HttpServletRequest request, HttpServletResponse response){
        //获取cookie
        String ticket= updatePersonalInfo.getTicket();
        if(ticket !=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(null!=userVo){
                //获取登录用户的手机号，根据手机号作修改
                Long phone=userVo.getUserPhone();
                updatePersonalInfo.setPhone(phone);
                return userService.updateUserInfo(updatePersonalInfo);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("修改部门成员信息")
    @PostMapping("/org/update")
    @ResponseBody
    public RespBean updateOrgUser(@RequestBody UpdatePersonalInfo updatePersonalInfo,HttpServletRequest request, HttpServletResponse response){
        //获取cookie
        String ticket= updatePersonalInfo.getTicket();
        if(ticket !=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(null!=userVo ){
                //获取登录用户的手机号，根据手机号作修改
                String action=userVo.getAction();
                if((action.equals("system_administrator")|| action.equals("organization_administrator"))){
                    //companyName非root时只能修改本部门的信息
                    return userService.updateUserInfo(updatePersonalInfo,userVo.getCompanyName());
                }
                return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"无操作权限");
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION,"未登录");
    }

    @ApiOperation("/获取部门成员(系统管理员获取用户)")
    @PostMapping("/org/userlist")
    @ResponseBody
    public RespBean getOrgCommonUserList(@RequestBody String ticket,HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(null!=userVo ){
                String action=userVo.getAction();//用于区分系统和部门
                String companyName=userVo.getCompanyName();
                return userService.getOrgCommonUserList(action, companyName);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION,"未登录");
    }
}
